﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using DTCash.Logger;
using Newtonsoft.Json;

namespace Uranus.WeiStationAPI.Common
{
    /// <summary>
    /// 配置中心
    /// </summary>
    public class ConfigCenterWork
    {
        /// <summary>
        /// 
        /// </summary>
        private static readonly string _configCenterUrl = ConfigurationManager.AppSettings["ConfigCenterUrl"];
        
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <param name="url"></param>
        private static ConfigCenterResponse GetCenterResponse(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentException("url is empty");
            }
            ConfigCenterResponse configCenterResponse = new ConfigCenterResponse();
            try
            {
                string str = Uranus.Common.RequestCommon.SimulationHttp(url, "", "get");
                configCenterResponse = JsonConvert.DeserializeObject<ConfigCenterResponse>(str) ??
                                       new ConfigCenterResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogError("请求配置中心，获取配置数据失败", string.Format("url:{0}", url), ex, true);
            }
            return configCenterResponse;
        }

        /// <summary>
        /// 获取配置数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private static ConfigResponse GetConfigResponse(string url)
        {
            ConfigCenterResponse configCenterResponse = GetCenterResponse(url);
            if (configCenterResponse.Code == 1 && configCenterResponse.Data != null)
            {
                try
                {
                    return JsonConvert.DeserializeObject<ConfigResponse>(
                        configCenterResponse.Data.ToString()) ??
                           new ConfigResponse();
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLogError("获取配置失败，序列化配置数据失败", string.Format("configCenterResponse.Data:{0}", configCenterResponse.Data), ex, true);
                }
            }
            return null;
        }

        /// <summary>
        /// 根据类型获取配置数据
        /// </summary>
        /// <param name="operationType"></param>
        /// <returns></returns>
        public static ConfigResponse GetConfigByOperationType(string operationType)
        {
            string url = GetOperationConfigUrl(operationType);
            return GetConfigResponse(url);
        }

        /// <summary>
        /// 根据类型获取配置数据
        /// </summary>
        /// <param name="operationType"></param>
        /// <returns></returns>
        public static ConfigCenterResponse GetResponseByOperationType(string operationType)
        {
            string url = GetOperationConfigUrl(operationType);
            return GetCenterResponse(url);
        }

        /// <summary>
        /// 根据类型获取配置的value值
        /// </summary>
        /// <returns></returns>
        public static object GetConfigValueByOperationType(string operationType)
        {
            string url = GetOperationConfigUrl(operationType);
            ConfigResponse configCenterResponse = GetConfigResponse(url);
            if (configCenterResponse != null && configCenterResponse.OperationStatus == 1)
            {
                return configCenterResponse.Value;
            }
            return null;
        }

        /// <summary>
        /// 根据类型获取配置的value值
        /// </summary>
        /// <param name="operationType"></param>
        /// <returns></returns>
        public static object GetConfigValueByOperationType(int operationType)
        {
            return GetConfigValueByOperationType(operationType.ToString());
        }

        /// <summary>
        /// 获取自定义的配置
        /// </summary>
        /// <param name="route"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static ConfigCenterResponse GetCustomResponse(string route, string param)
        {
            string url = GetCustomConfigUrl(route, param);
            return GetCenterResponse(url);
        }

        /// <summary>
        /// 根据类型获取配置
        /// </summary>
        /// <param name="operationType"></param>
        /// <returns></returns>
        private static string GetOperationConfigUrl(string operationType)
        {
            return string.Format("{0}/getconfig/{1}", _configCenterUrl, operationType);
        }

        /// <summary>
        /// 获取自定义配置
        /// </summary>
        /// <param name="route"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        private static string GetCustomConfigUrl(string route, string param)
        {
            string strParam = !string.IsNullOrEmpty(param) ? string.Format("?{0}", param) : string.Empty;
            return string.Format("{0}{1}{2}", _configCenterUrl, route, strParam);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class ConfigCenterResponse
    {
        /// <summary>
        /// 响应码
        /// </summary>
        [JsonProperty("code")]
        public int Code { get; set; }

        /// <summary>
        /// 响应体
        /// </summary>
        [JsonProperty("data")]
        public object Data { get; set; }

        /// <summary>
        /// 响应信息
        /// </summary>
        [JsonProperty("msg")]
        public string Msg { get; set; }
    }

    /// <summary>
    /// 配置响应实体
    /// </summary>
    public class ConfigResponse
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("_id")]
        public string ObjectId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("operationtype")]
        public int OperationType { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("operationstatus")]
        public int OperationStatus { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("createtime")]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("desc")]
        public string Desc { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("value")]
        public object Value { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        [JsonProperty("starttime")]
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        [JsonProperty("endtime")]
        public DateTime EndTime { get; set; }
    }
}